<?php
require_once "../config/app.config.php";
$entityBll = new EntityBLL();
$result = array();
$jsonResult = "";
//$user = new UserBLL();
//$user->AuthenticateUser("tibor.kiray@gmail.com", "12345");
if(isset($_REQUEST["task"]) && strtolower($_REQUEST["task"]) == "entitydetails"){
	$entity = $entityBll->GetBaseByID($_REQUEST["__id"]);
	$temp ="[{ID : '".$entity->GetID()."', Label: '".$entity->GetLabel()."'}]";
	$entityFieldsDAO = new EntityFieldDAO();
	$fields = $entityFieldsDAO->GetAllByEntityID($_REQUEST["__id"], "FormLocation", true, true);
	$result["Entity"] = $temp;
	$result["Success"] = true;

	$fieldsJson = "";
	$fieldProps = "";
	//vp($fields);
	foreach($fields as $field){
		$fieldProps .= ", {ID : '".$entity->getID()."', type : '".$field->getFieldType()."', name : '".$field->getID()."'";
		//vp($field->GetExtProperties($field->getID()));
		foreach($field->GetExtProperties($field->getID()) as $key=>$val){
			$fieldProps .= ", ".$key.": '".$val."'";
		}
		
		$fieldProps .="}";
		
		$custFieldProps .= ", {ID : '".$entity->getID()."', type : '".$field->getFieldType()."', name : '".$field->getID()."'";
		foreach($field->GetCustomClientProperties($field->getID()) as $key=>$val){
			$custFieldProps .= ", ".$key.": '".$val."'";
		}
		$custFieldProps .="}";
	}
	$result["ExtFields"] = "[".substr($fieldProps, 1)."]";
	$result["CustomClientFields"] = "[".substr($custFieldProps, 1)."]";

	$result = stripslashes('('.json_encode($result).')');
	?>
	<!--
	/*
	<?
		vp($custFieldProps);
	?>
	*/-->
	<?
	print $result;



}else if(isset($_REQUEST["task"]) && strtolower($_REQUEST["task"]) == "save"){
	$status = EntityBLL::AddRecord($_POST, $_FILES);
	$result = array("success"=>$status["success"], "message"=>$status["message"]);
	print json_encode($result);



}else if(isset($_REQUEST["task"]) && strtolower($_REQUEST["task"]) == "editrecord"){
	$entity = $entityBll->GetBaseByID($_REQUEST["__id"]);
	
	$temp ="[{ID : '".$entity->GetID()."', Label: '".$entity->GetLabel()."'}]";
	$entityFieldsDAO = new EntityFieldDAO();
	$fields = $entityFieldsDAO->GetAllByEntityID($_REQUEST["__id"], "FormLocation", true, true);
	$result["Entity"] = $temp;
	$result["Success"] = true;

	$fieldsJson = "";
	$fieldProps = "";
	//vp($fields);
	foreach($fields as $field){
		$fieldProps .= ", {ID : '".$entity->getID()."', type : '".$field->getFieldType()."', name : '".$field->getID()."'";
		//vp($field->GetExtProperties($field->getID()));
		foreach($field->GetExtProperties($field->getID()) as $key=>$val){
			$fieldProps .= ", ".$key.": '".$val."'";
		}
		
		$fieldProps .="}";
		
		$custFieldProps .= ", {ID : '".$entity->getID()."', type : '".$field->getFieldType()."', name : '".$field->getID()."'";
		foreach($field->GetCustomClientProperties($field->getID()) as $key=>$val){
			$custFieldProps .= ", ".$key.": '".$val."'";
		}
		$custFieldProps .="}";
	}
	$result["ExtFields"] = "[".substr($fieldProps, 1)."]";
	$result["CustomClientFields"] = "[".substr($custFieldProps, 1)."]";

	$result = stripslashes('('.json_encode($result).')');
	?>
	<!--
	/*
	<?
		vp($custFieldProps);
	?>
	*/-->
	<?
	print $result;
}
?>